package com.zbkj.admin.util;

import cn.hutool.crypto.SecureUtil;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;

public class RC4Util {

    private static final String key = "LTAI5t5miit1AvwakeuPdVUZ";

    /**
     * RC4加密（结果Base64编码）
     *
     * @param input 明文
     * @return Base64编码的密文
     */
    public static BigInteger encrypt(Long input) {
        String string = SecureUtil.rc4(key).encryptHex(String.valueOf(input));
        return new BigInteger(String.valueOf(hexToNumManual(string)));
    }

    /**
     * RC4解密
     *
     * @param base64Input Base64编码的密文
     * @return 解密后的明文
     */
    public static String decrypt(long base64Input) {
        return SecureUtil.rc4(key).decrypt(bigNumToHex(String.valueOf(base64Input)));
    }

    public static long hexToNumManual(String s) {
        s = s.trim().toLowerCase();
        if (s.startsWith("0x")) {
            s = s.substring(2);
        }
        long result = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            int val;
            if (Character.isDigit(c)) {
                val = c - '0';
            } else {
                val = c - 'a' + 10; // 字母 a-f 转为 10-15
            }
            result = result * 16 + val;
        }
        return result;
    }

    public static String bigNumToHex(String decimalStr) {
        BigInteger num = new BigInteger(decimalStr);
        return num.toString(16); // 小写，无前缀
    }


    // 测试用例
    public static void main(String[] args) {
        System.out.println(encrypt(1475L));
        System.out.println(decrypt(306054652L));
//        System.out.println(hexToNumManual(SecureUtil.rc4(key).encryptHex("3544")));
//        System.out.println(bigNumToHex("272566013"));
//        System.out.println(bigNumToHex("305792765"));
//        System.out.println(bigNumToHex("1717003"));
//        System.out.println(encrypt(1044L));
//        System.out.println(SecureUtil.rc4(key).decrypt("EjoG/Q=="));
//        System.out.println(decrypt(5001932687675571517L)); // 217786682 EjoGQ
/*        String key = "testKey";
        String original = "5869147";
        String encrypted = encrypt(original);
        String code = generateInviteCode(original);
        System.out.println("加密结果：" + encrypted);
        System.out.println("生成邀请码：" + code); // 示例：3HqV9A

        String decoded = decodeInviteCode(code);
        System.out.println("解码结果：" + decoded); // 应输出5869147*/

//        String encrypted = encrypt(original);
//        String string = generateInviteCode(original);
//        String decrypt = decodeInviteCode(string);
//        System.out.println(encrypted);
//        System.out.println(string);
//        System.out.println(decrypt);
//        System.out.println("加密结果：" + generateInviteCode(original)); // 输出：8J7g8fK2x7k=

//        String decrypted = decrypt(encrypted);
//        System.out.println("解密结果：" + decrypted); // 输出：Hello World!
    }
}
